package com.flink.flinkdemo.demo;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.ReduceOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Random;

/**
 * @author zhangy on 2021/1/4
 * @description: add you description
 */
public class CountRiskDealTime {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> source = env.readTextFile("/Users/zhangyong/Idea-git/flink-d/flink-demo/src/doc/export_1610076757");


        SingleOutputStreamOperator<Tuple3<Double, Integer, Double>> oper = source.flatMap(new FlatMapFunction<String, Tuple3<Double, Integer, Double>>() {

            @Override
            public void flatMap(String value, Collector<Tuple3<Double, Integer, Double>> out) throws Exception {
                String[] words = value.split(",");
                for (String word : words) {
                    if (word.contains("\\\"dealTime\\\":")) {
                        String[] split = word.split(":");
                        if (split.length > 1) {
                            Double d = Double.valueOf(split[1].replace("}\"}", ""));
                            out.collect(new Tuple3<>(Double.valueOf(0), 1, d));
                        } else {
                            System.out.println(word);
                        }

                    }
                }

            }
        });
//        flatMap.writeAsText("/Users/zhangyong/Idea-git/flink-d/flink-demo/src/doc/res"+ Random.);
        SingleOutputStreamOperator<Tuple3<Double, Integer, Double>> reduce = oper.keyBy(0).reduce(new ReduceFunction<Tuple3<Double, Integer, Double>>() {
            @Override
            public Tuple3<Double, Integer, Double> reduce(Tuple3<Double, Integer, Double> value1, Tuple3<Double, Integer, Double> value2) throws Exception {
                return new Tuple3<>(value1.f0 + value2.f2, value1.f1 + 1, Double.valueOf((value1.f0 + value2.f2) / (value1.f1 + 1)));
            }
        });


        reduce.print();


//        SingleOutputStreamOperator<Tuple2<Integer, String>> oper = source.flatMap(new FlatMapFunction<String, Tuple2<Integer, String>>() {
//            @Override
//            public void flatMap(String value, Collector<Tuple2<Integer, String>> out) throws Exception {
//                String[] words = value.split(",");
//                for (String word : words) {
//                    if (word.contains("\\\"dealTime\\\":")) {
//                        String[] split = word.split(":");
//                        if (split.length > 1) {
//                            Integer d = Integer.valueOf(split[1].replace("}\"}", ""));
//                            out.collect(new Tuple2<>(d, value));
//                        } else {
//                            System.out.println(word);
//                        }
//
//                    }
//                }
//            }
//        });
//        oper.print();
//        SingleOutputStreamOperator<Tuple2<Integer, String>> filter = oper.filter(new FilterFunction<Tuple2<Integer, String>>() {
//            @Override
//            public boolean filter(Tuple2<Integer, String> value) throws Exception {
//                return value.f0.compareTo(1000) > 0 ? true : false;
//            }
//        });
//        filter.print();
        env.execute();

    }
}